package com.bigevent.bigevent.service.impl;

import com.bigevent.bigevent.common.CommonVal;
import com.bigevent.bigevent.entity.User;
import com.bigevent.bigevent.mapper.UserMapper;
import com.bigevent.bigevent.service.UserService;
import com.bigevent.bigevent.utils.JwtUtil;
import com.bigevent.bigevent.utils.Md5Util;
import com.bigevent.bigevent.utils.ThreadLocalUtil;
import com.bigevent.bigevent.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author yangxudong
 * @description: 用户service实现类
 * @date 2024/2/3 12:52
 */
@Service
public class UserSeriviceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //Override the register method to create a registration for the user
    @Override
    public void register(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null) {
            throw new RuntimeException("已存在该用户名");
        }
        userMapper.register(username, Md5Util.getMD5String(password));
    }


    @Override
    //根据用户名查找用户
    public String login(String username, String password) {
        //根据用户名查找用户
        User user = findByUserName(username);
        //判断密码是否正确
        if (!user.getPassword().equals(Md5Util.getMD5String(password))) {
            //不正确则抛出异常
            throw new RuntimeException("密码错误");
        }
        //正确则创建一个Map用于存储用户的用户名和id
        Map<String, Object> claims = new HashMap<>();
        //存储用户名
        claims.put("username", username);
        //存储用户id
        claims.put("userId", user.getId());
        //返回生成的token
        String token = JwtUtil.genToken(claims);
        // 存入redis
        redisTemplate.opsForValue().set(CommonVal.REDIS_TOKEN_KEY + token, token, 30, TimeUnit.MINUTES);
        return token;
    }

    @Override
    public User getUser(String username) {
        return findByUserName(username);
    }

    @Override
    public void updateUserInfo(User user) {
        String username = UserUtil.getUsername();
        if (username!= null && !username.equals(user.getUsername())) {
            throw new RuntimeException("用户名与当前登录用户不一致");
        }
        User queryUser = findByUserName(user.getUsername());
        queryUser.setEmail(user.getEmail());
        queryUser.setNickname(user.getNickname());
        userMapper.updateEmailAndNickName(queryUser);
    }

    @Override
    public void updateAvatar(String username, String avatarUrl) {
        User queryUser = findByUserName(username);
        queryUser.setUserPic(avatarUrl);
        userMapper.updatePicUrl(queryUser);
    }

    @Override
    public void updatePwd(String username, String oldPwd, String newPwd, String rePwd, String token) {
        User queryUser = findByUserName(username);
        //验证旧密码是否正确
        if (!queryUser.getPassword().equals(Md5Util.getMD5String(oldPwd))) {
            throw new RuntimeException("旧密码错误");
        }
        //验证新密码和确认密码是否一致
        if (!newPwd.equals(rePwd)) {
            throw new RuntimeException("新密码和确认密码不一致");
        }
        //修改密码
        queryUser.setPassword(Md5Util.getMD5String(newPwd));
        userMapper.updatePwd(queryUser);
        // 让当前已登录的用户失效
        redisTemplate.opsForValue().getOperations().delete(CommonVal.REDIS_TOKEN_KEY + token);
    }


    private User findByUserName(String username) {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        return user;
    }

}
